home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / stadconv.lzh / STADCONV / TOSTAD.S < prev   
Text File  |  1992-07-11  |  11KB  |  200 lines

  1. *****************************************************************************
  2. *                                                                           *
  3. * Ass-Subroutine to convert a 32k-picture (screenformat) to a packed        *
  4. * STAD-format.                                                              *
  5. *                                                                           *
  6. * params:   (sp) LONG  return-adress                                        *
  7. *          4(sp) LONG  source-adress (32k buffer)                           *
  8. *          8(sp) LONG  dest-adress of STAD-pic (33k buffer)                 *
  9. *                                                                           *
  10. * returns:   do  WORD  length of packed STAD-picture                        *
  11. *                                                                           *
  12. * important: the two adresses are not removed from the stack (like trap)    *
  13. *                                                                           *
  14. * History:                                                                  *
  15. * Version 1.0   01.06.1991  Wolfgang Ley                                    *
  16. *                           - first version                                 *
  17. * Version 1.1   03.06.1991  Wolfgang Ley                                    *
  18. *                           - saving registers                              *
  19. *                           - improved statistics                           *
  20. *                           - no extra statistics-buffer anymore            *
  21. * Version 1.2   05.06.1991  Wolfgang Ley                                    *
  22. *                           - extra counter in v_pack removed               *
  23. * Version 1.3   25.06.1992  Jens Dittmer                                    *
  24. *                           - bug in h_pack removed (header)                *
  25. * Version 1.4   29.06.1992  Jens Dittmer                                    *
  26. *                           - algorithm improved -> much smaller code       *
  27. * Version 1.5   01.07.1992  Jens Dittmer                                    *
  28. *                           - improved statistics                           *
  29. * Version 1.5b  11.07.1992  Wolfgang Ley                                    *
  30. *                           - extra rts removed                             *
  31. *                                                                           *
  32. *****************************************************************************
  33.  
  34.                 TEXT
  35.  
  36.                 movem.l D1-A1,-(SP)     ;save registers
  37.  
  38.                 movea.l 40(SP),A0       ;adress of 32k-picture
  39.                 movea.l 44(SP),A1       ;adress of packes STAD-picture
  40.  
  41. ;----------------------------------------------------------------------------
  42. ; make some staticts (get most frequently used and the two less used bytes)
  43. ; --> choose PACK-/ID-/SPECIAL-byte
  44. ;----------------------------------------------------------------------------
  45.  
  46.                 move.w  #511,D0         ;index-register
  47. stat_1:         clr.b   0(A1,D0.w)      ;clear byte-counters
  48.                 dbra    D0,stat_1
  49.  
  50.                 move.w  #31999,D0       ;pointer within the picture
  51. stat_2:         move.b  0(A0,D0.w),D1   ;get byte
  52.                 add.w   D1,D1           ;double it
  53.                 andi.w  #$01FF,D1       ;clear the rest
  54.                 addq.w  #1,0(A1,D1.w)   ;and count byte
  55.                 dbra    D0,stat_2       ;and so on...
  56.  
  57.                 move.w  #510,D0         ;most frequently used byte till now
  58.                 move.w  0(A1,D0.w),D1   ;get quantity
  59.                 move.w  #508,D2         ;counter to the rest
  60. stat_3:         cmp.w   0(A1,D2.w),D1   ;compare
  61.                 bhs.s   stat_4
  62.                 move.w  D2,D0           ;remember position
  63.                 move.w  0(A1,D2.w),D1   ;and remember the quantity
  64. stat_4:         subq.w  #2,D2           ;next entry
  65.                 bpl.s   stat_3          ;and again...
  66.                 lsr.w   #1,D0           ;that's the PACK-byte
  67.                 move.b  D0,D3           ;PACK-byte to D3
  68.  
  69.                 move.w  #510,D0         ;less used byte till now
  70.                 move.w  0(A1,D0.w),D1   ;get quantity
  71.                 move.w  #508,D2         ;counter to the rest
  72. stat_5:         cmp.w   0(A1,D2.w),D1   ;compare
  73.                 blo.s   stat_6
  74.                 move.w  D2,D0           ;remember position
  75.                 move.w  0(A1,D2.w),D1   ;...and the quantity
  76.                 beq.s   stat_7          ;number=0 => exit loop
  77. stat_6:         subq.w  #2,D2           ;next counter
  78.                 bpl.s   stat_5          ;and continue...
  79. stat_7:         move.w  #32001,0(A1,D0.w) ;"clear" entry
  80.                 lsr.w   #1,D0           ;that's the ID-bByte
  81.                 move.b  D0,D4           ;ID-byte to D4
  82.  
  83.                 move.w  #510,D0         ;like above...
  84.                 move.w  0(A1,D0.w),D1
  85.                 move.w  #508,D2         
  86. stat_8:         cmp.w   0(A1,D2.w),D1
  87.                 blo.s   stat_9
  88.                 move.w  D2,D0
  89.                 move.w  0(A1,D2.w),D1
  90.                 beq.s   stat_a
  91. stat_9:         subq.w  #2,D2
  92.                 bpl.s   stat_8
  93. stat_a:         lsr.w   #1,D0           ;that's the SPEC-byte
  94.                 move.b  D0,D5           ;SPEC-Byte to D5
  95.  
  96. ;----------------------------------------------------------------------------
  97. ; pack horizontal and vertical, use smaller one as STAD-picture
  98. ; (validate if the compressed picture is really smaller - if not, use the
  99. ;  uncompressed picture)
  100. ;----------------------------------------------------------------------------
  101.  
  102.                 clr.w   D0              ;clear offset
  103.                 move.b  #'p',0(A1,D0.w) ;write header #'pM85'
  104.                 move.b  #'M',1(A1,D0.w) ;(bytes, because address may be odd)
  105.                 move.b  #'8',2(A1,D0.w) ;'5' or '6' is appended at packing
  106.                 move.b  D4,4(A1,D0.w)   ;write ID-Byte
  107.                 move.b  D3,5(A1,D0.w)   ;write PACK-Byte
  108.                 move.b  D5,6(A1,D0.w)   ;write SPEC-Byte
  109.  
  110.                 bsr.s   h_pack          ;pack horizontal
  111.                 move.w  D0,D7           ;remember length (horiz.)
  112.                 bsr.s   v_pack          ;pack vertical
  113.                 cmp.w   D7,D0           ;vertical smaller?
  114.                 bls.s   check           ;yes -> everything ok
  115.                 bsr.s   h_pack          ;no  -> pack again horizontal
  116. check:          cmpi.w  #32000,D0       ;is the packed picture really
  117.                 blo.s   exit            ;smaller?
  118.                 move.w  #31999,D0       ;if not, copy original 32k-pic
  119. copy:           move.b  (A0)+,(A1)+    
  120.                 dbra    D0,copy
  121.                 move.w  #32000,D0       ;length is 32000 bytes
  122. exit:           movem.l (SP)+,D1-A1     ;restore registers
  123.                 rts                     ;that's it
  124.  
  125. ;----------------------------------------------------------------------------
  126. ; horizontal compress
  127. ;----------------------------------------------------------------------------
  128.  
  129. h_pack:         clr.w   D6              ;source-index
  130.                 moveq   #7,D0           ;length of header
  131.                 move.b  #'5',3(A1,D6.w)
  132. h_pack_1:       clr.w   D1              ;sequence-length 0
  133.                 move.b  0(A0,D6.w),D2   ;read byte
  134. h_pack_2:       addq.w  #1,D6           ;next column
  135.                 cmpi.w  #32000,D6       ;ready?
  136.                 beq.s   emit            ;write code and terminate
  137.                 cmpi.w  #255,D1         ;max. seq.-length?
  138.                 beq.s   h_pack_3        ;write code and start new sequence
  139.                 cmp.b   0(A0,D6.w),D2   ;next source-byte <> seq.byte?
  140.                 bne.s   h_pack_3        ;write code and start new sequence
  141.                 addq.w  #1,D1           ;increment length
  142.                 bra.s   h_pack_2        ;and read next...
  143. h_pack_3:       bsr.s   emit            ;store sequence
  144.                 bra.s   h_pack_1        ;and start new sequence
  145.  
  146. ;-------------------------------------------------------